import 'package:flutter/material.dart';
import 'package:interview/utils/http.dart';
import 'package:interview/utils/logger.dart';

/// QuestionType 类型定义
class QuestionType {
  final int id;
  final String name;
  final int displayNewestFlag;

  QuestionType({
    required this.id,
    required this.name,
    required this.displayNewestFlag,
  });

  factory QuestionType.fromJson(Map<String, dynamic> json) {
    return QuestionType(
      id: json['id'],
      name: json['name'],
      displayNewestFlag: json['displayNewestFlag'],
    );
  }
}

/// 获取试题分类
Future<List<QuestionType>> fetchQuestionTypes(BuildContext context) async {
  try {
    // 发起 GET 请求
    final response = await Http.get<Map<String, dynamic>>(
      'question/type', 
      context: context
    );
    // 检查响应结构
    if (response['success'] == true && response['data'] is List) {
      return (response['data'] as List).map((item) => QuestionType.fromJson(item as Map<String, dynamic>)).toList();
    } else {
      throw Exception('响应数据格式错误或未成功: ${response['message']}');
    }
  } catch (e) {
    // 错误处理
    Logger.error("获取试题分类失败: $e", tag: 'fetchQuestionTypes');
    throw Exception('获取试题分类失败: $e');
  }
}
